import 'package:flutter/material.dart';

class KeyItem extends StatefulWidget {
  final List<String> currentList;
  final Function tap;
  final Function delete;
  final Color color;
  final String text;
  const KeyItem(
      {Key key,
      @required this.tap,
      @required this.text,
      this.currentList,
      this.color = Colors.black,
      this.delete})
      : super(key: key);

  @override
  _KeyItemState createState() => _KeyItemState();
}

class _KeyItemState extends State<KeyItem> {
  bool isTap = false;

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return ClipRRect(
      borderRadius: BorderRadius.circular(4),
      child: Material(
        child: Ink(
          decoration: new BoxDecoration(
            color: Colors.white,
          ),
          child: InkResponse(
            onTap: () {
              if (widget.text == 'delete') {
                widget.delete();
              } else {
                widget.tap();
              }
            },
            borderRadius: new BorderRadius.all(new Radius.circular(25.0)),
            child: Container(
              height: 44,
              decoration: new BoxDecoration(),
              alignment: Alignment.center,
              width: _getWidth(width),
              child: Text(
                _getText(),
                style: TextStyle(
                  color: widget.text == 'delete' ? Colors.red : widget.color,
                  fontSize: width / 375 * 18,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  double _getWidth(width) {
    switch (widget.text) {
      case 'delete':
        return width / 375 * 50;
        break;
      default:
        if (widget.currentList == null) {
          return 60;
        }
        return width / (widget.currentList.length + 2);
    }
  }

  String _getText() {
    switch (widget.text) {
      case 'delete':
        return '删除';
        break;
      default:
        return widget.text;
    }
  }
}
